var pid = '';
define(function(require,exports,module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var A = require("resource/js/component/ajax.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var ajax = require("resource/js/component/ajax.js");
	var urltool = require("resource/js/component/urlTool.js");
	require("resource/js/component/rules.js");
	var config = {
			column:[[
		         {field:'number',title:'number',width:70,align:'center',height:50,hidden:true},
		         {field:'start',title:'start',width:70,align:'center',height:50,hidden:true},
		         {field:'end',title:'end',width:70,align:'center',height:50,hidden:true},
		         {field:'containAttachment',title:'',width:10,align:'center',height:50,hidden:true},
		         {field:'priority',title:'状态',width:20,align:'left',height:50,formatter:function(value, row, index) {
		        	 var html = '';
		        	 if (value=="普通") {
		        		 html += '<span style="padding-left: 21px;"></span>';
		        	 } else {
		        		 html += '<img src="../../../resource/images/urgent1.png" title="紧急" style="border-radius: 0;width: 13px;height: 13px;padding-left: 8px;" />';
		        	 }
		        	 if (row.containAttachment==true) {
		        		 html += '<img src="../../../resource/images/annex-file.png" title="包含附件" style="border-radius: 0;width: 16px;height: 16px;padding-left: 8px;" />';
		        	 } else {
		        		 html += '';
		        	 }
		        	 return html;
		         }},
		         {field:'toAddressList',title:'收件人',width:100,align:'left',height:50, formatter: function(value, row, index) {
		        	 if(value) {
		        		 var addr = '';
		        		 for (var i = 0; i < value.length; i++) {
		        			 if (value[i].personal != null && value[i].personal != "") {
		        				 addr += value[i].personal + ";"
		        			 } else {
		        				 addr += value[i].email + ";"
		        			 }
						}
		        		 if (addr != '') {
		        			 addr = addr.substring(0, addr.length - 1);
		        		 } else {
		        			 return '(未填写）';
		        		 }
		        		return addr;
		        	 }
		         }},
		         {field:'subject',title:'主题',width:260,align:'left',height:50},
		         {field:'sendDate',title:'创建时间',width:60,align:'center',height:50},
			]]
		};
	var dgutil = undefined;
	var datagridconfig = undefined;
	var positionData = [
	    {positionVal: 1, positionName: "主题"},
	    {positionVal: 2, positionName: "正文"}
	];
	function initPage() {
		ajax.init().success(function(code,msg,data){
			if(code==0){
				$("#position").combobox({
					valueField : 'positionVal',
					textField: 'positionName',
					editable: false
				});
				$("#position").combobox("loadData", positionData);
				$("#position").combobox("select", 1);
				
				using(["datebox"], function() {
					$("#sendDateStart").datebox({
						prompt: '请选择开始时间',
						editable: false
					});
					$("#sendDateEnd").datebox({
						prompt: '请选择结束时间',
						editable: false
					});
				});
				dgutil = new easyutil.ajaxDataGrid(datagridconfig);
			} 
		}).error(function(code,msg,data) {
			$.messager.confirm('系统提示', msg, function(r){
	             if (r){
	            	 addEmail();
	             }
	        });
		}).post(BASEPATH + "/oa/email/checkUserMailExist.htm");
	}
	
	function compareDate(DateOne,DateTwo){
    	var dt = DateTwo;
        var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));  
        var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);  
        var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));  
      
        var TwoMonth = dt.substring(5,dt.lastIndexOf ('-'));  
        var TwoDay = dt.substring(dt.length,dt.lastIndexOf ('-')+1);  
        var TwoYear = dt.substring(0,dt.indexOf ('-'));  
      
        if (Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)> Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear)){
        	return false;
        } else {
        	return true;
        }
    }

	function addEmail(){
		$('#addEmaildlg').dialog({
			iconCls: 'icon-save',
			title:'添加邮箱账号',
			href: BASEPATH + '/view/oa/email/addEmail',
			onLoad: function() {
				$("#emailSuffix").text(emailSuffix);
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addEmailForm").form("validate")){
							//验证邮箱是否重复
							var email = $("#email").textbox("getValue");
							email += emailSuffix;
							ajax.init().success(function(code, msg, data){
								if (code == 0) {
									ajax.init().success(function(code, msg, data){
										if (code == 0) {
											dgutil = new easyutil.ajaxDataGrid(datagridconfig);
											$("#addEmaildlg").dialog("close");
											$.messager.show({
												title:'',
												msg: msg,
												timeout:1000,
												showType:'slide'
											});
										}
									}).error(function(code, msg, data){
										$.messager.show({
											title:'',
											msg: msg,
											timeout:1000,
											showType:'slide'
										});
									}).post(BASEPATH+'/oa/email/saveUserMail.htm', {email: email});
								}
							}).error(function(code, msg, data) {
								$.messager.show({
									title:'',
									msg: msg,
									timeout:1000,
									showType:'slide'
								});
							}).post(BASEPATH+'/oa/email/checkUserMailRepeat.htm', {email: email});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#addEmaildlg').dialog('close');
				}
			}]
		});
		$("#addEmaildlg").dialog("open");
	}
	
	//删除
	var delInit=function(baseUrl,rows){
		var checks="";
		for(var key in rows){
			if(key==rows.length-1){
				checks+=rows[key].number;
			}else{
				checks+=rows[key].number+",";
			}
		}
		easyutil.ajaxHandler(baseUrl,{numbers: checks, folderType: 3},function(){
			dgutil.delRow(rows);
		});
	}

	module.exports = {
		init : function() {
			pid = this.pid||urltool.getParam("pid");
			var buttons = {
				title: {
					_class: "",
					text: "草稿箱列表"
				},
				btn: [
				{
					iconCls:"icon-search",
					text:"查询",
					addClass:"l-btn-display",
					url:"/oa/email/getDraftEmail.htm",
					handler:function(){
						var sendDateStart = $('#sendDateStart').datebox('getValue');
						var sendDateEnd = $('#sendDateEnd').datebox('getValue');
						if (sendDateStart !="" && sendDateEnd !="" && !compareDate(sendDateStart, sendDateEnd)) {
							$.messager.show({
								title:'',
								msg: "开始时间不能大于结束时间",
								timeout:1000,
								showType:'slide'
							});
						} else {
							dgutil.pager(1);
						}
					}
				 },
				{
					iconCls: 'icon-remove',
					url:'/oa/email/delBatchEmail.htm',
					text:"删除",
					addClass:'l-btn-display',
					needRows:true,
					isCheck:true,
					handler: delInit
				} ]
			};
			datagridconfig = {
				fitColumns: true,
				singleSelect: false,
				striped: true,
				autoRowHeight: false,
				fit: true,
				url: BASEPATH + '/oa/email/getDraftEmail.htm',
				toolbar: buttons,
				columns: config.column,
				eid: "dg",
				args:"searchform",
				selectOnCheck: true,
				checkOnSelect: false,
				onClickRow:function(index,row){
					$(this).datagrid("uncheckRow",index);
					
					window.location.href = BASEPATH + "/view/oa/email/sendEmail?pid="+pid+"&sendType=draftSend&number="+row.number+"&folderType="+3;
				}
			};
			using(["parser","datagrid", "combobox", "datebox"], function() {
				initPage();
			});
		}
	}
});
